<?php if ( ! defined('BASEPATH')) exit('This file does not run directly. Sorry.'); ?>
<?php
/**
 * Kalaisoo.
 *
 * A lightweight no-framework framework written in PHP.
 *
 * @package Kalaisoo
 * @author $Author: info@sah-company.com $
 * @version $Id: admin.php 126 2012-01-11 20:59:15Z info@sah-company.com $
 */

/**
 * Kalaisoo Admin Controller Class.
 *
 * This is the admin controller of our framework. It has methods and views to let admins add
 * users, roles, actions, permissions and other settings of our web application.
 *
 * @package Kalaisoo
 * @subpackage Controller
 * @author $Author: info@sah-company.com $
 * @see sys/core/refused.php class Controller
 */
class Admin_Controller extends Controller
{
	/**
	 * constructs a new Admin_Controller and initializes it.
	 */
	public function __construct()
	{
		parent::__construct();
		$this->init();
	}
	
	/**
	 * starts a session and loads needed files.
	 */
	protected function init()
	{
		session_start();
		$_SESSION['lastActivity'] = time();
		$this->registry->cache->setLifetime(0);
		$this->registry->loader->lib(array('redbean', 'permission'));
		$this->registry->loader->model('user');
		$this->registry->loader->language('admin');
		$this->registry->loader->helper(array('url', 'textile'));
		$this->permission = new Permission();
		$this->template = new Template();
		$this->input = new Input();
		$this->user = R::dispense('user');
		$this->data['scripts'][] = 'jquery/jquery-1.6.2.min';
		$this->data['scripts'][] = 'jquery/ui/minified/jquery.effects.core.min';
		$this->data['scripts'][] = 'jquery/ui/minified/jquery.effects.blind.min';
		$this->data['scripts'][] = 'core';
		$this->auth();
	}
	
	
	/**
	 * checks if the current session is authenticated and load the user or redirects to login.
	 */
	protected function auth()
	{
		if ( ! $this->user->isAuth()) {
			return $this->redirect(ahref(sprintf('%s/user/login/?goto=%s', $this->registry->language, urlencode($this->registry->router->url()))));
		}
		$this->user = R::load('user', $_SESSION['auth']);
		$this->data['user'] = $this->user;
	}
	
	/**
	 * displays the administrator index page.
	 */
	public function index()
	{
		if ( ! $this->permission->granted($this->user, 'index', 'admin')) {
			return $this->error(__('error_no_permission'));
		}
		$this->template->view('admin/index', $this->data);
	}
}
?>